package com.example.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.common.api.ResultCode;
import com.example.common.api.ResultUtil;
import com.example.user.mapper.UserMapper;
import com.example.user.model.dto.UserDTO;
import com.example.user.model.po.UserPO;
import com.example.user.model.vo.UserVO;
import com.example.user.service.IUserService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.DigestUtils;

import java.util.*;

@Service
@Slf4j
public class UserService extends ServiceImpl<UserMapper, UserPO> implements IUserService {

    @Resource
    private UserMapper userMapper;

    @Override
    public ResultUtil createUser(UserVO userVO) {
        log.info("create user begin, receive params: {}", userVO.toString());

        Map<String, Object> map = new HashMap<>();
        map.put("mobile", userVO.getMobile());

        List<UserPO> list = userMapper.selectByMap(map);
        if (!CollectionUtils.isEmpty(list)){
            log.info("create user, user mobile is exist");
            return ResultUtil.failed(ResultCode.MOBILE_EXIST);
        }

        //不设置密码，默认是123456
        if (StringUtils.isBlank(userVO.getPassword())){
            userVO.setPassword("123456");
        }

        //使用Spring自带的MD5加密
        String password = DigestUtils.md5DigestAsHex(userVO.getPassword().getBytes());

        UserPO userPO = new UserPO();
        BeanUtils.copyProperties(userVO, userPO);
        userPO.setPassword(password);

        int row = userMapper.insert(userPO);

        if (row > 0){
            UserDTO userDTO = new UserDTO();
            BeanUtils.copyProperties(userPO, userDTO);

            log.info("create user success end!");
            return ResultUtil.success(userDTO);
        }

        log.error("create user fail, mobile {}", userPO.getMobile());
        return ResultUtil.failed(ResultCode.CREATE_USER_ERROR);
    }

    @Override
    public ResultUtil updateUser(UserVO userVO) {
        log.info("update user begin, receive params: {}", userVO.toString());

        UserPO userPO = new UserPO();
        BeanUtils.copyProperties(userVO, userPO);

        if (userVO.getPassword() != null){
            //使用Spring自带的MD5加密
            String password = DigestUtils.md5DigestAsHex(userVO.getPassword().getBytes());
            userPO.setPassword(password);
        }

        int row = userMapper.updateById(userPO);

        if (row > 0){
            UserDTO userDTO = new UserDTO();
            BeanUtils.copyProperties(userPO, userDTO);

            log.info("update user success end!");
            return ResultUtil.success(userDTO);
        }

        log.error("update user fail, mobile {}", userPO.getMobile());
        return ResultUtil.failed(ResultCode.UPDATE_USER_ERROR);
    }

    @Override
    public ResultUtil getUser(String mobile, String password) {
        log.info("get user begin, receive params: mobile: {}, password: {}",  mobile, password);

        //使用Spring自带的MD5加密
        String md5Password = DigestUtils.md5DigestAsHex(password.getBytes());

        Map<String, Object> map = new HashMap<>();
        map.put("mobile", mobile);
        map.put("password", md5Password);

        QueryWrapper<UserPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.allEq(map);

        UserPO userPO = userMapper.selectOne(queryWrapper);

        if (userPO != null){
            log.info("user information exist, user_id: {}", userPO.getId());
            log.info("get the role code by user ID begin");
            List<String> roleCode = userMapper.getRoleCodeByUserId(userPO.getId());
            log.info("get the role code by user ID end");

            UserDTO userDTO = new UserDTO();
            BeanUtils.copyProperties(userPO, userDTO);

            userDTO.setRoleCode(roleCode);

            log.info("get user end, get user success");

            return ResultUtil.success(userDTO);
        }

        log.info("get user end, get user empty");

        return ResultUtil.failed(ResultCode.ACCOUNT_OR_PASSWORD_ERROR);
    }
}
